﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using CUREEntity.Harmful;
using DBUtility;
using System.Web;
using System.Collections;
using CUREDataAccessLayers.Common;

namespace CURESendMail
{
    public class SendMail
    {
        private common comm = new common();
        /// <summary>
        /// 获取邮件发送参数
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string IsEMail()
        {
            string ls_return = "0";

            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select switchvalue from sys_config  where switchcode = 'IsEMail' ");
            try
            {
                dt = DbHelperOra.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    ls_return = dr[0].ToString().Trim();
                }
            }          
            return ls_return;
        }
        /// <summary>
        /// 获取邮件发送参数
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetParameter(string PName)
        {
            string Parameter = "";

            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select svalue from sys_config  where switchcode = '" + PName + "' ");
            try
            {
                dt = DbHelperOra.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    Parameter = dr[0].ToString().Trim();
                }
            }
            return Parameter;
        }
        /// <summary>
        /// 获取邮件发送参数
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetFid(string Start, string End)
        {
            string str = "F卡ID： ";

            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select fid from harmful_ledger a ");
            commandText.Append(" where a.createtime > to_date('" + Start + "', 'yyyy-MM-dd') ");
            commandText.Append(" and a.createtime < to_date('" + End + "', 'yyyy-MM-dd') ");
            try
            {
                dt = DbHelperOra.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    str += dr[0].ToString().Trim() + ";";
                }
            }
            return str;
        }
        /// <summary>
        /// 获取发件人列表
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public List<string> GetUser()
        {
            List<string> list = new List<string>();

            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select to_char(ID)||';'||Email from master where IsEmail=0 and Email is not null ");
            try
            {
                dt = DbHelperOra.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    list.Add(dr[0].ToString().Trim());
                }
            }
            return list;
        }
        /// <summary>
        /// 生成发送日志
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public bool InsertLog(string userID, string Email) 
        {
            bool lb_return = false;

            string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            StringBuilder commandText = new StringBuilder();
            commandText.Append(" insert into SendMail values('" + userID + "','" + Email + "',to_date('" + now + "','yyyy-MM-dd HH24:mi:ss'))");

            try
            {
                int res = DbHelperOra.ExecuteSql(commandText.ToString());
                if (res > 0)
                { lb_return = true; }
            }
            catch (Exception ex)
            {
                comm.InsertLog(ex.Message);
            }
            return lb_return;
        }
        //更新发送邮件时间
        public bool UpdateEMailSendTime() 
        {
            bool lb_return = false;
            string now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            StringBuilder commandText = new StringBuilder();
            commandText.Append(" update sys_config set svalue='" + now + "' where switchcode='EMailSendTime' ");
            try
            {
                int res = DbHelperOra.ExecuteSql(commandText.ToString());
                if (res > 0)
                { lb_return = true; }
            }
            catch (Exception ex)
            {
                comm.InsertLog(ex.Message);
            }
            return lb_return;
        }


    }
}
